package bookez.view.bean;

import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

public class UnicodeBundle extends ResourceBundle {
	
	public UnicodeBundle( ResourceBundle bundle ) {
		setParent( bundle );
		this.coreBundle = (PropertyResourceBundle) bundle;
	}//end constructor
	
	public UnicodeBundle( String bundleName ) {
		Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
		ResourceBundle bundle = ResourceBundle.getBundle( bundleName , locale );
		assert( bundle != null );
		this.coreBundle = ( PropertyResourceBundle ) bundle;
		setParent( bundle );
	}//end constructor

	@Override
	public Enumeration<String> getKeys() {
		// TODO Auto-generated method stub
		return coreBundle.getKeys();
	}//end method getKeys

	@Override
	protected Object handleGetObject( String key ) {
		// TODO Auto-generated method stub
		String value = (String) coreBundle.handleGetObject( key );
		if( value == null )  { return null; }
		try {
			return new String(value.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			return null;
		}//end catch
	}//end method handleObject
	
	private PropertyResourceBundle coreBundle;
}//end abstract class UnicodeBundle
